package com.guodi.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.minio.util.MinioUtil;
import com.guodi.model.ueditor.ActionEnter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 用于处理关于ueditor插件相关的请求
 * @author Guoqing
 * @date 2017-11-29
 *
 */
@RestController
@RequestMapping("/uediter")
@Api(value = "API - UeditorController", tags = "百度富文本编辑器后台controller")
public class UeditorController extends BaseController {

	private final String fileDir = "onemap/model/ueditor";

	@RequestMapping("/exec")
	@ApiOperation(value = " 根据实体属性分页查询")
	public AjaxResult exec(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String path = request.getSession().getServletContext().getRealPath("/");
		String action = request.getParameter("action");
		String callback = request.getParameter("callback");

		if (action.equals("config")) {
			response.setContentType("application/json;charset=utf-8");
			request.setCharacterEncoding("utf-8");
			PrintWriter writer = null;
			try {
				String exec = new ActionEnter(request, path).exec();
				String call = callback + "(" + exec + ")";
				//这里之所以用PrintWriter对象，是因为springmvc里配了消息转换器之后，
				//使用@ResponseBody注解会将返回的json字符串用\转义，这样前端无法解析
				writer = response.getWriter();
				writer.print(call);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				writer.close();
			}
			return null;
		}
		return null;
	}

	@PostMapping(value = "/exec")
	public AjaxResult exec(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "upfile", required = false) MultipartFile upfile) {
		if (upfile != null) {
			String id = UuidUtil.get32UUID();
			PrintWriter writer = null;
			try {
				response.setContentType("application/json;charset=utf-8");
				request.setCharacterEncoding("utf-8");
//				String fileFormat = upfile.getOriginalFilename().substring(upfile.getOriginalFilename().lastIndexOf(".") + 1);
				MinioUtil.uploadFile(upfile.getInputStream(), fileDir+"/"+id + '/' + upfile.getOriginalFilename());

				JSONObject jsonObject = new JSONObject();
				jsonObject.put("state", "SUCCESS");
				jsonObject.put("original", upfile.getName());
				jsonObject.put("size", upfile.getSize());
				jsonObject.put("title", upfile.getOriginalFilename());
//				jsonObject.put("type",replace);
				jsonObject.put("url", fileDir+"/"+id + '/' + upfile.getOriginalFilename());
				writer = response.getWriter();
				//必須指定那個服務器上傳的ip或域名
				response.setHeader("Access-Control-Allow-Origin", "*");
				//支持跨域上傳
				response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,X_Requested_With");
				writer.print(jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				writer.close();
			}
			return null;
		}
		return null;
	}

}
